/*
 * File:   csNode.cpp
 * Author: gian
 *
 * Created on December 1, 2012, 6:33 PM
 */

#include "csNode.hpp"

csSegment::csSegment() {
  n1 = 0;
  n2 = 0;
}

csSegment::~csSegment() {
  if (n1 != 0) {
    n1 -> mConnections.remove(this);
  }

  if (n2 != 0) {
    n2 -> mConnections.remove(this);
  }
}

csSegment * csSegment::connect(csNode * n1,
                               csNode * n2) {
  csSegment * seg = new csSegment;

  n1 -> mConnections.push_back(seg);
  n2 -> mConnections.push_back(seg);

  seg -> n1 = n1;
  seg -> n2 = n2;

  return seg;
}

SegmentList csSegment::connections(csNode * n1,
                                   csNode * n2) {
  SegmentList connection_list;

  for (SegmentList::iterator p1 = n1 -> mConnections.begin(); p1 != n1 -> mConnections.end(); p1++) {
    for (SegmentList::iterator p2 = n2 -> mConnections.begin(); p2 != n2 -> mConnections.end(); p2++) {
      if (*p1 == *p2) {
        connection_list.push_back(*p1);
      }
    }
  }

  return connection_list;
}

csNode::csNode() {}

csNode::csNode(const csNode & orig) {}

csNode::~csNode() {}

csSegment * csNode::connection(const int i) {
  SegmentList::iterator p = mConnections.begin();

  for (int j = 0; i < i; j++) {
    p++;
  }

  return *p;
}

csNode * csNode::connected_node(const int i) {
  SegmentList::iterator p = mConnections.begin();

  for (int j = 0; i < i; j++) {
    p++;
  }

  csSegment * s = *p;

  return (s -> node1() == this) ? s -> node2() : s -> node1();
}

int csNode::connections() {
  return mConnections.size();
}


//~ Formatted by Jindent --- http://www.jindent.com
